package com.art.sunflower.service.impl;

import com.art.sunflower.enumeration.RoleType;
import com.art.sunflower.exception.NullStudentException;
import com.art.sunflower.mapper.StudentMapper;
import com.art.sunflower.mapper.UserMapper;
import com.art.sunflower.service.StudentService;
import com.art.sunflower.util.Md5SaltTool;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentMapper studentMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public MyPageInfo getList(Integer pageSize, Integer pageNo) throws NullStudentException {
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> student = studentMapper.selectList();
        if (student.size() == 0)
            throw new NullStudentException();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(student);
        return new MyPageInfo(pageInfo);
    }

    @Override
    public void add(String username, String password, String phone, String name, String sex, Integer year, String parentName, Integer classroomId)
            throws NoSuchAlgorithmException, NullStudentException {
        userMapper.insertUserAll(username, Md5SaltTool.getEncryptedPwd(password), RoleType.student, name, phone);
        Integer id = userMapper.selectLastInsertId();
        studentMapper.insertStudent(id, sex, year, parentName, classroomId);
    }

    @Override
    public void update(Integer id, String username, String password, String phone, String name, String sex, Integer year, String parentName, Integer classroomId) throws NoSuchAlgorithmException, NullStudentException {
        Map<String, Object> student = studentMapper.selectStudent(id);
        if (student == null) {
            throw new NullStudentException();
        } else {
            userMapper.updateUser((Integer) student.get("user_id"), username, Md5SaltTool.getEncryptedPwd(password), name, phone);
            studentMapper.updateStudent(id, sex, year, parentName, classroomId);
        }
    }

    @Override
    public void delete(Integer id) throws NullStudentException {
        Map<String, Object> student = studentMapper.selectStudent(id);
        if (student == null) {
            throw new NullStudentException();
        } else {
            studentMapper.delete(id);
        }
    }

    @Override
    public Map<String, Object> getById(Integer id) throws NullStudentException {
        Map<String, Object> student = studentMapper.selectStudent(id);
        if (student == null) {
            throw new NullStudentException();
        } else {
            return studentMapper.selectById(id);
        }
    }

    public List<Integer> getTeacherClassroomStudent(Integer userId) {
        return studentMapper.selectTeacherClassroomStudent(userId);
    }

    public List<Integer> getTeacherClassesStudent(Integer userId) {
        return studentMapper.selectTeacherClassesStudent(userId);
    }

}
